<template>
	<!-- 用户消息 -->
	<view>
		<view class="newlist" v-for="(item,index) in list" :key="index" @click="handlenavigate(item)">
			<view v-for="(i,t) in key[index]" :key="t" v-if="key[index].length-1 == t">
				<view class="headPortrait flex-cb" >
					<view class="flex align-center img">
						<image :src="i.avatar?url+i.avatar:'/static/index/1.png'" mode=""></image>
						<text>{{i.nickname||"微信用户"}}</text>
					</view>
					<text class="cuIcon-right" style="margin-right: 20rpx;"></text>
				</view>
				<!-- <view class="" >
					<view class="newContent" >{{i.message}}</view>
				</view>
				<view class="time">{{i.time}}</view> -->
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				list:[],
				arr:[],
				url:'',
				go:0,
				key:[],
				new:'',//未在线信息
				imdata:{"message":"","avatar":"","nickname":"","send_uid":'',"time":''},
				newarr:[]
			};
		},
		onLoad() {
			this.url = this.$base
			this.$api.messages_unread().then(res=>{
				console.log(res);
				this.new = res.data
			})
			
		},
		onShow() {
			if(this.go == 0){
				
			}
			this.list = []
			setTimeout(()=>{
				this.get()
			},500)
		},
		onPullDownRefresh() {
			this.list = []
			this.get()
			uni.stopPullDownRefresh()
		},
		methods:{
			handlenavigate(deta){
				this.go = 1
				uni.navigateTo({
					url:'./chat?uid='+deta
				})
			},
			get(){
				uni.getStorageInfo({
					success:res=> {
						let that = this
						console.log(res.keys);
						for (let i = 0; i < res.keys.length; i++) {
							if(Number(res.keys[i])){
								this.list.push(res.keys[i])
							}
						}
						if(that.list.length !=0){//存储有数据
							console.log(1);
							for (let i = 0; i < that.list.length; i++) {
								if(that.new.length !=0){//未读消息有数据返回
									console.log(2);
									if(that.new[i].send_uid == that.list[i] &&Array.isArray(uni.getStorageSync(""+that.list[i]))){//存储有数据是数组 且等于未读消息id 
										for (let j = 0; j < that.new[i].length; j++) {
											for (let k = 0; k < that.new[j].messages.length; k++) {
												that.imdata.avatar = that.new[j].avatar
												that.imdata.nickname = that.new[j].nickname
												that.imdata.send_uid = that.new[j].send_uid
												that.imdata.message = that.new[j].messages[k].message
												that.imdata.time = that.new[j].messages[k].created_at
												that.newarr.push(that.imdata)
												that.imdata = {"message":"","avatar":"","nickname":"","send_uid":'',"time":''}
											}
										}
										that.key[i] = uni.getStorageSync(""+that.list[i])
										that.key[i] = [...that.key[i],...that.newarr]
										
										uni.setStorage(""+that.list[i],that.key[i])
									}else if(that.new[i].send_uid == that.list[i] && !Array.isArray(uni.getStorageSync(""+that.list[i]))){//存储有数据不是数组 且等于未读消息id 
										that.key[i] = [uni.getStorageSync(""+that.list[i])]
										that.key[i] = [...that.key[i],...that.newarr]
										uni.setStorage(""+that.list[i],that.key[i])
									}
								}else{//未读消息没有数据
									console.log(11);
									if(Array.isArray(uni.getStorageSync(""+that.list[i]))){
										that.key[i] = uni.getStorageSync(""+that.list[i])
									}else{
										that.key[i] = [uni.getStorageSync(""+that.list[i])]
									}
								}
							}
						}else{//存储无数据 未读消息有数据
							for (let t = 0; t < that.new.length; t++) {
								that.list.push(that.new[t].send_uid)
								for (let k = 0; k < that.new[t].messages.length; k++) {
									that.imdata.avatar = that.new[t].avatar
									that.imdata.nickname = that.new[t].nickname
									that.imdata.send_uid = that.new[t].send_uid
									that.imdata.message = that.new[t].messages[k].message
									that.imdata.time = that.new[t].messages[k].created_at
									that.newarr.push(that.imdata)
									that.imdata = {"message":"","avatar":"","nickname":"","send_uid":'',"time":''}
								}
								console.log(that.newarr);
								that.key[t] = [...that.newarr]
								console.log('1111');
								uni.setStorage({
									key:""+that.new[t].send_uid,
									data:that.key[t],
									success(res) {
										console.log(res);
									},
									fail(err) {
										console.log(err);
									}
								})
							}
						}
					}
				})
			}
		}
	}
</script>

<style lang="scss">
.newlist{
	width: 710rpx;
	height: 130rpx;
	background: #FFFFFF;
	border-radius: 10rpx;
	margin:20rpx auto;
	margin-bottom: 20rpx;
	padding: 0 0 0 51rpx;
	.headPortrait{
		padding-top: 20rpx;
		image{
			width: 89rpx;
			height: 89rpx;
		}
		.img{
			font-size: 34rpx;
			font-weight: bold;
			text{
				margin-left: 13rpx;
			}
		}
		.cuIcon-title{
			font-size: 40rpx;
			color: #FF2E2E;
			margin-right: 43rpx;
		}
	}
	.newContent{
		width: 616rpx;
		height: 79rpx;
		font-size: 28rpx;
		font-family: PingFang SC-Regular, PingFang SC;
		font-weight: 400;
		color: #919AA5;
		overflow: hidden;
		text-overflow: ellipsis;
		line-height: 39rpx;
		margin-top: 30rpx;
		margin-bottom: 35rpx;
	}
	.time{
		color: #A7A6A9;
		font-size: 26rpx;
	}
}
</style>
